package com.mediastory.spsserver.customer.web;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.support.MessageSourceAccessor;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.ServletRequestUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.mediastory.spsserver.app.service.AppSvc;
import com.mediastory.spsserver.customer.domain.Customer;
import com.mediastory.spsserver.customer.service.CustomerSvc;
import com.mediastory.spsserver.users.service.UsersSvc;

/**
 * @author Jaewon Choi
 */

@Controller
@RequestMapping("/customer")
public class CustomerCtrl {
	
	private final Logger logger = Logger.getLogger(getClass());
	
	@Autowired
	private MessageSourceAccessor messages;
	
	@Autowired
	private CustomerSvc customerSvc;
	
	@Autowired
	private UsersSvc usersSvc;
	
	@Autowired
	private AppSvc appSvc;
	
	/**
	 * 목록 보기
	 * */
	@RequestMapping(value={"/list.do"})
	public Map<String, Object> list(HttpServletRequest request) throws Exception {
		Map<String, Object> condition = new HashMap<String, Object>();
		//검색 parameter
		String name = ServletRequestUtils.getStringParameter(request, "name");
		String appIds = ServletRequestUtils.getStringParameter(request, "appIds");
		condition.put("name", name);
		condition.put("appIds", appIds);

		//페이징 처리 parameter
		int maxnum = ServletRequestUtils.getIntParameter(request, "maxnum", 10);
		int page = ServletRequestUtils.getIntParameter(request, "page", 1);
		condition.put("maxnum", maxnum);
		condition.put("page", (page-1)*maxnum);
		
		Map<String, Object> resultMap = new HashMap<String, Object>();
		resultMap.putAll(customerSvc.findbyListPaging(condition));
		resultMap.put("maxnum", maxnum);
		resultMap.put("page", page);
		
		return resultMap;
	}

	/**
	 * 등록 폼, 콘텐츠 상세 보기
	 * */
	@RequestMapping(value={"/form.do"})
	public Map<String, Object> form(HttpServletRequest request) throws Exception {
		Map<String, Object> resultMap = new HashMap<String, Object>();

		int idx = ServletRequestUtils.getIntParameter(request, "idx", 0);
		if(idx != 0){
			resultMap.put("customer", customerSvc.findby(idx));
			
			Map<String, Object> condition = new HashMap<String, Object>();
			condition.put("cIdx", idx);
			resultMap.put("usersList", usersSvc.findbyList(condition));
		}
		
		Map<String, Object> condition = new HashMap<String, Object>();
		resultMap.put("appList", appSvc.findbyList(condition));
		
		return resultMap;
	}
	
	/**
	 * 콘텐츠 등록/수정
	 * */
	@RequestMapping(value={"/submit.do"}, method=RequestMethod.POST)
	public String submit(Customer customer, HttpSession session, HttpServletRequest request) throws Exception {
		String userId = (String)session.getAttribute("userId");
		String ip = request.getRemoteAddr();
		String name = customer.getName();
		
		System.out.print("name:"+name);
		
		if(name == null || (name!=null && name.length() < 1 && name.length() > 20)){
			throw new Exception("[ERROR] 잡지사명은 1글자 이상 20 글자 이하로 작성해주세요.name:"+name);
		}
		
		if(customer.getIdx() != null){
			customer.setModifiedId(userId);
			customer.setModifiedIp(ip);
			customerSvc.modify(customer);
		}else{
			customer.setCreatedId(userId);
			customer.setCreatedIp(ip);
			customerSvc.add(customer);
		}

		return "customer/result";
	}
	
	/**
	 * 콘텐츠 삭제
	 * */
	@RequestMapping(value={"/remove.do"}, method=RequestMethod.POST)
	public String remove(HttpServletRequest request, HttpSession session) throws Exception {
		
		String userId = (String)session.getAttribute("userId");
		
		int[] idx = ServletRequestUtils.getIntParameters(request, "idx");
		
		Map<String, Object> condition = new HashMap<String, Object>();
		
		condition.put("modifiedId", userId);
		condition.put("idx", idx);
		
		customerSvc.remove(condition);
		return "customer/result";
	}

	@RequestMapping("/chkDupl.do")
	public @ResponseBody String chkDuplication(HttpSession session, @RequestParam(required=true, value="name")  String name) throws Exception {
		Map<String, Object> condition = new HashMap<String, Object>();
		condition.put("chkDupl", true);
		condition.put("name", name);
		return customerSvc.findbyChkDupl(condition);
	}

}